import { ReactElement, useContext, useState, useEffect } from 'react'
import { Row, Col, Form, Select, Input } from 'antd'
import style from './index.module.less'
import { Context } from '../../context'
import UnitNumberInput from '@components/unitNumberInput'
import TextCell from '@src/components/textCell'
import Document from '../document'
import { awaitWrap } from '@src/assets/js/tool'
import { getDocumentListRequest } from './ajax'
import { CaseDocumentFullSchema } from '@components/schema/caseSchema'

const { Item } = Form
const { Option } = Select
const { TextArea } = Input

// 初始化可选文件列表
async function initDocumentList (setList: (v: CaseDocumentFullSchema[]) => void): Promise<void> {
  const [e, d] = await awaitWrap(getDocumentListRequest())
  if (e === null && d instanceof Array) {
    setList(d)
  }
}

function Main (): ReactElement {
  const [list, setList] = useState<CaseDocumentFullSchema[]>([])
  const { caseDetail: d } = useContext(Context)
  const disabled = d !== null && ['已抽查'].includes(d.score_status)
  useEffect(() => {
    initDocumentList(setList).catch(e => console.error(e))
  }, [])
  return (
    <div className={style.container} id='inputBlockFive'>
      <div className={style.title}>办案抽查</div>
      <Row>
        <Col span={10}>
          <Item label='1. 延迟制作/填写程序文书'>
            {disabled
              ? (
              <Item name='processer_notice_list' noStyle><TextCell formatFn={(li) => `${li.title as string} - ${li.count as number}天`} /></Item>
                )
              : Document('processer_notice_list', list, disabled)}
          </Item>
        </Col>
        <Col span={1}><span /></Col>
        <Col span={9}>
          <Item label='3. 结案文书撰写一般性文字错误' name='typo_count'>
            {disabled ? <TextCell unit='处' /> : <UnitNumberInput disabled={disabled} unit='处' />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={9}>
          <Item label='4. 结案文书撰写计算错误' name='miscalculate_count'>
            {disabled ? <TextCell unit='处' /> : <UnitNumberInput disabled={disabled} unit='处' />}
          </Item>
        </Col>
        <Col span={2}><span /></Col>
        <Col span={9}>
          <Item label='5. 漏发仲裁材料影响程序进展' name='mismaterial_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={9}>
          <Item label='6. 错发仲裁材料/违反工作规范给单位仲裁院当事人造成经济损失' name='mispostmaterial_score'>
            {disabled ? <TextCell unit='分' /> : <UnitNumberInput disabled={disabled} max={-1} min={-10} unit='分' precision={2} />}
          </Item>
        </Col>
        <Col span={2}><span /></Col>
        <Col span={9}>
          <Item label='7. 故意/过失仪式或损毁重要案卷材料的' name='misfile_count'>
            {disabled ? <TextCell unit='次' /> : <UnitNumberInput disabled={disabled} unit='次' />}
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={20}>
          <Item label='8. 结案文书程序遗漏'>
            <Row gutter={8}>
              <Col flex='auto'>
                <Item noStyle name='misprocedure_desc'>
                  {
                    disabled
                      ? <TextCell />
                      : (
                        <Select placeholder='请选择' mode='multiple'>
                          <Option value='管辖权异议'>管辖权异议</Option>
                          <Option value='反请求的受理'>反请求的受理</Option>
                          <Option value='程序变更'>程序变更</Option>
                          <Option value='开庭审限延长'>开庭审限延长</Option>
                        </Select>
                        )
                  }
                </Item>
              </Col>
              <Col flex='160px'>
                <Item name='misprocedure_count' noStyle>
                  {disabled ? <TextCell unit='次' /> : <UnitNumberInput placeholder='合计次数' />}
                </Item>
              </Col>
            </Row>
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={20}>
          <Item label='9. 实体性遗漏'>
            <Row gutter={8}>
              <Col flex='auto'>
                <Item noStyle name='misarbtity_desc'>
                  {
                    disabled
                      ? <TextCell />
                      : (
                        <Select placeholder='请选择' mode='multiple'>
                          <Option value='证据'>证据</Option>
                          <Option value='案件事实'>案件事实</Option>
                          <Option value='法律问题认定'>法律问题认定</Option>
                        </Select>
                        )
                  }
                </Item>
              </Col>
              <Col flex='160px'>
                <Item name='misarbtity_count' noStyle>
                  {disabled ? <TextCell unit='次' /> : <UnitNumberInput placeholder='合计次数' />}
                </Item>
              </Col>
            </Row>
          </Item>
        </Col>
      </Row>
      <Row>
        <Col span={20}>
          <Item label='10. 其他疏漏事项视具体情况扣分'>
            <Row gutter={8}>
              <Col flex='auto'>
                <Item name='misabc_desc' noStyle>
                  {disabled ? <TextCell /> : <TextArea placeholder='请输入' rows={2} allowClear />}
                </Item>
              </Col>
              <Col flex='160px'>
                <Item name='misabc_score' noStyle>
                  {disabled ? <TextCell unit='分' /> : <UnitNumberInput disabled={disabled} max={-1} min={-5} unit='分' precision={2} />}
                </Item>
              </Col>
            </Row>
          </Item>
        </Col>
      </Row>
    </div>
  )
}

export default Main
